package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author :XingJiaCheng
 * @date :2022-23-20-15:23
 * @description
 **/
@Slf4j
@RestController
@RequestMapping("/category")
@Api("员工管理接口")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @PostMapping

    public R<String>save(@RequestBody Category category){
log.info("添加菜品:{}",category);
        categoryService.save(category);
        return R.success("添加成功");
    }
    @GetMapping("/page")
    public R<Page>findByPage(Integer page,Integer pageSize){
        log.info("page:{}  pageSize:{}",page,pageSize);
        Page<Category> infoPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Category::getSort);
        categoryService.page(infoPage, wrapper);
        return R.success(infoPage);
    }
    @DeleteMapping
    public R<String>deleteById(String id){
        log.info("被删除的id:{}", id);
        categoryService.removeCategory(id);
        return R.success("删除成功");

    }
    @PutMapping
    public R<String>update(@RequestBody Category category){
        log.info("修改分类:{}",category);
        categoryService.updateById(category);
        return R.success("修改成功");
    }
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        log.info("[分类的展示 category:{}]",category);
        // 调用service,根据条件获取数据
        // select * from category where type = 1 order by sort,update_time desc;
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper();
        // 如果type !=null 我们基于条件查询
        queryWrapper.eq(category.getType()!=null,Category::getType,category.getType());
        queryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> categoryList = categoryService.list(queryWrapper);
        return R.success(categoryList);
    }

}
